/**
 * markdown 文件转换为 js 文件
 * 供给 crudcode 使用
 */

const fs = require('fs');

const mdDir = '../', prefix = 'es_', barstr = '+'.repeat(20);
let pos = 0, sqlArr = [], title = '', content='';
const structureFile = './_0-structure.sql',sampleJsFile='./_0-Sample.js';

fs.readdirSync(mdDir).forEach(filename => {
    if (filename.startsWith('_') || !filename.endsWith('模块.md')) {
        console.log('[skip] ', filename);
        return;
    }
    content = fs.readFileSync(mdDir + filename).toString();
    pos = content.indexOf('```sql');
    if (pos == -1) {
        console.log('[skip] ', filename);
        return;
    }
    content = content.substring(pos + 6).trim();
    pos = content.indexOf('```');
    content = content.substring(0, pos).trim();
    content = content.replace(/(EXISTS|TABLE)\s+`*([a-z|0-9|_]+)`*/g, `$1 \`${prefix}$2\``);
    title = filename.replace('.md', '');
    content = `\n-- ${barstr} ${title} ${barstr}\n\n${content}\n`;
    sqlArr.push(content);
    console.log('[ok] ', filename);
})

fs.writeFileSync(structureFile, sqlArr.join("\n"));
fs.writeFileSync(sampleJsFile,`/**
* 获取演示数据库 SQL 脚本
*/
function getExampleSql() {
if (typeof (getMySql) == "function") {
return getMySql();
}
return \`
-- ----------------------------
-- 管理员管理
-- ----------------------------
DROP TABLE IF EXISTS es_auth_admin;
CREATE TABLE IF NOT EXISTS es_auth_admin (
id INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
username VARCHAR(60) NOT NULL COMMENT '用户名',
nickname VARCHAR(60) NOT NULL DEFAULT '' COMMENT '昵称',
realname VARCHAR(60) NOT NULL DEFAULT '' COMMENT '姓名',
password VARCHAR(32) NOT NULL COMMENT '密码',
salt VARCHAR(10) NOT NULL DEFAULT '' COMMENT '密码盐',
avatar VARCHAR(200) NOT NULL DEFAULT '' COMMENT '头像',
email VARCHAR(100) NOT NULL DEFAULT '' COMMENT '电子邮箱',
mobile VARCHAR(11) NOT NULL DEFAULT '' COMMENT '手机号码',
gender TINYINT UNSIGNED NOT NULL DEFAULT 3 COMMENT '性别:1=男,2=女,3=未知',
login_count INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '登录次数',
login_time INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '登录时间',
login_ip VARCHAR(46) NOT NULL DEFAULT '' COMMENT '登录IP',
register_ip VARCHAR(46) NOT NULL DEFAULT '' COMMENT '注册IP',
status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态:1=正常,2=禁用,3=待审核,4=审核驳回',
created_at INT UNSIGNED NOT NULL COMMENT '注册时间',
updated_at INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
PRIMARY KEY (id),
UNIQUE KEY uk_username (username),
KEY k_mobile (mobile)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='管理员管理?module=权限';

-- ----------------------------
-- 角色管理
-- ----------------------------
DROP TABLE IF EXISTS es_auth_role;
CREATE TABLE IF NOT EXISTS es_auth_role (
id INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
pid INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '上级ID',
title VARCHAR(60) NOT NULL DEFAULT '' COMMENT '标题',
name VARCHAR(30) NOT NULL COMMENT '名称',
status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态:1=正常,2=禁用',
created_at INT UNSIGNED NOT NULL COMMENT '创建时间',
updated_at INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色管理?module=权限';

-- ----------------------------
-- 菜单管理
-- ----------------------------
DROP TABLE IF EXISTS es_auth_menu;
CREATE TABLE IF NOT EXISTS es_auth_menu (
id INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
pid INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '上级ID',
title VARCHAR(60) NOT NULL COMMENT '标题',
controller VARCHAR(60) NOT NULL DEFAULT '' COMMENT '控制器名称',
action VARCHAR(60) NOT NULL DEFAULT '' COMMENT '操作名称',
type TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '类型:1=分类,2=菜单,3=按钮',
icon VARCHAR(200) NOT NULL DEFAULT '' COMMENT '菜单图标',
sort SMALLINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '排序权重',
status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态:1=正常,2=禁用',
created_at INT UNSIGNED NOT NULL COMMENT '创建时间',
updated_at INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
PRIMARY KEY (id),
KEY k_pid (pid),
KEY k_sort (sort)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜单管理?module=权限';
\`
}

// 业务SQL
function getMySql() {
return \`
${sqlArr.join().replaceAll('\`','')}\`
}

export default getExampleSql;`);